package com.yunxi.service.dubboService.impl.vehicle;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.vehicle.VehicleSpectrum;
import com.yunxi.service.localService.dealer.PartnerService;
import com.yunxi.service.localService.vehicle.VehicleSpectrumService;
import com.yunxi.util.RedisCacheService;
import com.yunxi.vehicle.pojo.VehicleSpectrumReqVO;
import com.yunxi.vehicle.pojo.VehicleSpectrumRespVO;
import com.yunxi.vehicle.pojo.VehicleSpectrumVO;
import com.yunxi.vehicle.service.VehicleSpectrumDubboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 生产车型型谱 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-16
 */
public class VehicleSpectrumDubboServiceImpl implements VehicleSpectrumDubboService {

    @Autowired
    @Qualifier(value = "vehicleSpectrumServiceImpl")
    VehicleSpectrumService vehicleSpectrumService;

    @Autowired
    RedisCacheService redisCacheService;

    @Autowired
    @Qualifier(value = "partnerServiceImpl")
    PartnerService partnerService;

    @Override
    @Cacheable(value="vehicle",key = "'vehicle:spectrum'")
    public Object findTreeList() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
       return vehicleSpectrumService.findTreeList();
    }

    @Override
    public Object pageByVo(VehicleSpectrumVO vehicleSpectrum, Integer pageNum, Integer pageSize) {
        return vehicleSpectrumService.pageByVo(vehicleSpectrum,pageNum,pageSize);
    }

    @Override
    public void removeById(String productId) {
        vehicleSpectrumService.removeById(productId);
    }

    @Override
    public void saveOrUpdate(VehicleSpectrumVO vehicleSpectrum) {
        VehicleSpectrum v = new VehicleSpectrum();
        BeanUtils.copyProperties(vehicleSpectrum,v);
        vehicleSpectrumService.saveOrUpdate(v);

        redisCacheService.deleteKeys("vehicle::vehicle:spectrumAlias");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumFull");
    }

    @Override
    public Object getById(String productId) {
        return vehicleSpectrumService.getById(productId);
    }

    @Override
    @Cacheable(value="vehicle",key = "'vehicle:spectrumAlias'")
    public Object treeAlias() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return vehicleSpectrumService.treeAlias();
    }

    @Override
    @Cacheable(value="vehicle",key = "'vehicle:spectrumFull'")
    public Object treeFull() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return vehicleSpectrumService.treeFull();
    }

    @Override
    public VehicleSpectrumVO loadByNodeCode(String nodeCode) {
        return vehicleSpectrumService.loadByNodeCode(nodeCode);
    }

    @Override
    public Object findTreeList(LoginInfo loginInfo) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if(StringUtils.isNotEmpty(loginInfo.getPartnerCode())){
            return vehicleSpectrumService.findTreeList(loginInfo);
        }
        return vehicleSpectrumService.findTreeList();
    }

    @Override
    public Object findTreeList(String partnerCode) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setPartnerCode(partnerCode);

        PartnerVO vo = partnerService.getByPartnerCode(partnerCode);
        loginInfo.setPartner(vo);
        return vehicleSpectrumService.findTreeList(loginInfo);
    }

    /**
     * BOM基础车型
     * @param vo
     * @return
     */
    @Override
    public PageInfo<VehicleSpectrumRespVO> queryVehicleSpectrumByApp(VehicleSpectrumReqVO vo, Integer pageNum, Integer pageSize) {
        return vehicleSpectrumService.queryVehicleSpectrumByApp(vo,  pageNum,  pageSize);
    }

    @Override
    public List<VehicleSpectrumRespVO> queryVehicleSpectrumByApp(VehicleSpectrumReqVO vo){
        return vehicleSpectrumService.queryVehicleSpectrumByApp(vo);
    }

    @Override
    public List<VehicleSpectrumRespVO> queryVehicleSpectrumByAppLast(VehicleSpectrumReqVO vo){
        return vehicleSpectrumService.queryVehicleSpectrumByAppLast(vo);
    }
}
